package com.getpebble.android.framework.protocol.inbound;

import com.getpebble.android.R;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.util.ByteUtils;
import com.sun.mail.smtp.SMTPMessage;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class PblInboundLogDumpMessage extends PblInboundMessage {
    private final byte[] mCookie;
    private final Log mLog;
    private final ResponseType mResponseType;

    public static class Log {
        private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        public final String filename;
        public final int level;
        public final int lineNumber;
        public final String message;
        public final long timestamp_seconds;

        public Log(long timestamp_seconds, int level, String filename, int lineNumber, String message) {
            this.timestamp_seconds = timestamp_seconds;
            this.level = level;
            this.filename = filename;
            this.message = message;
            this.lineNumber = lineNumber;
        }

        private String logLevelToString(int logLevel) {
            switch (logLevel) {
                case R.styleable.WalletFragmentStyle_buyButtonHeight /*0*/:
                    return "*";
                case SMTPMessage.RETURN_FULL /*1*/:
                    return "E";
                case 50:
                    return "W";
                case 100:
                    return "I";
                case 200:
                    return "D";
                case 255:
                    return "V";
                default:
                    return "?";
            }
        }

        private static String formatTimestamp(long t_seconds) {
            Date timeStampDate = new Date(1000 * t_seconds);
            DATE_FORMATTER.setTimeZone(TimeZone.getTimeZone("UTC"));
            return DATE_FORMATTER.format(timeStampDate);
        }

        public String getFormattedMessage() {
            return String.format("%s %s %s:%s> %s", new Object[]{logLevelToString(this.level), formatTimestamp(this.timestamp_seconds), this.filename, Integer.valueOf(this.lineNumber), this.message});
        }

        public static Log getLogMessage(ByteBuffer buf, int responseLength) {
            CharsetDecoder asciiDecoder = Charset.forName("US-ASCII").newDecoder();
            return new Log((long) buf.getInt(), buf.get() & 255, ByteUtils.getStringFromBuffer(buf, 16), buf.getShort() & 65535, ByteUtils.getStringFromBuffer(buf, buf.get() & 255));
        }
    }

    public enum ResponseType {
        LOG(Byte.MIN_VALUE),
        DONE((byte) -127),
        STATS_DUMP_DONE((byte) -125);
        
        final byte id;

        private ResponseType(byte id) {
            this.id = id;
        }
    }

    EndpointId getId() {
        return EndpointId.LOG_DUMP;
    }

    protected int getMinSize() {
        return 5;
    }

    public PblInboundLogDumpMessage(ProtocolMessage message) throws IllegalArgumentException {
        super(message);
        ByteBuffer buffer = message.getDataBuffer();
        byte typeId = buffer.get();
        if (typeId == ResponseType.DONE.id) {
            this.mResponseType = ResponseType.DONE;
            this.mCookie = new byte[4];
            buffer.get(this.mCookie);
            this.mLog = null;
        } else if (typeId == ResponseType.LOG.id) {
            this.mResponseType = ResponseType.LOG;
            this.mCookie = new byte[4];
            buffer.get(this.mCookie);
            this.mLog = Log.getLogMessage(buffer, buffer.remaining());
        } else if (typeId == ResponseType.STATS_DUMP_DONE.id) {
            this.mResponseType = ResponseType.STATS_DUMP_DONE;
            this.mCookie = new byte[4];
            buffer.get(this.mCookie);
            this.mLog = null;
        } else {
            throw new IllegalArgumentException("Invalid type: " + typeId);
        }
    }

    public ResponseType getResponseType() {
        return this.mResponseType;
    }

    public byte[] getCookie() {
        return this.mCookie;
    }

    public Log getLog() {
        return this.mLog;
    }
}
